standingUp = new window.Animation "standingUp", [[3, 0]]
standingDown = new window.Animation "standingDown", [[0, 0]]
standingLeft = new window.Animation "standingLeft", [[5, 1]]
standingRight = new window.Animation "standingRight", [[0, 1]]

runningUp = new window.Animation "runningUp", [[3, 0], [4, 0], [3, 0], [5, 0]]
runningUp.fps 10
runningDown = new window.Animation "runningDown", [[0, 0], [1, 0], [0, 0], [2, 0]]
runningDown.fps 10
runningRight = new window.Animation "runningRight", [[0, 1], [1, 1], [0, 1], [2, 1]]
runningRight.fps 10
runningLeft = new window.Animation "runningLeft", [[5, 1], [4, 1], [5, 1], [3, 1]]
runningLeft.fps 10

spr = new window.Sprite(16, 16)
spr.addAnimation standingUp
spr.addAnimation standingDown
spr.addAnimation standingLeft
spr.addAnimation standingRight

spr.addAnimation new window.Animation "playerDead", [[6, 0]]

spr.addAnimation runningUp
spr.addAnimation runningDown
spr.addAnimation runningLeft
spr.addAnimation runningRight
spr.current = spr.standingDown
inp = window.Input

class window.Player extends window.Entity
  boffx : -> 3 * 4
  boffy : -> 10 * 4
  bwidth : -> 10 * 4
  bheight : -> 6 * 4


  constructor : ->
    @waiting = spr.current
    @walkingSpeed = 2.0
    @runningSpeed = 5.0
    @pickupCooldown = 0
    @edx = 0
    @edy = 0
    @ghost = false

  init : (@g) ->
    super(@g)
    @addTag "player"


  update : () ->
    @edx *= 0.90
    @edy *= 0.90
    spr.current = spr.playerDead if @isDead()
    if @isDead()
      if inp.any.isDown()
        @g.restore()

      return

    dx = 0
    dy = 0

    if inp.left.isDown()
      spr.current = spr.runningLeft 
      @waiting = spr.standingLeft
      dx = -1

    if inp.right.isDown()
      spr.current = spr.runningRight if inp.right.isDown()
      @waiting = spr.standingRight if inp.right.isDown()
      dx = 1

    if inp.up.isDown()
      spr.current = spr.runningUp if inp.up.isDown()
      @waiting = spr.standingUp if inp.up.isDown()
      dy = -1

    if inp.down.isDown()
      spr.current = spr.runningDown if inp.down.isDown()
      @waiting = spr.standingDown if inp.down.isDown()
      dy = 1

    mdx = dx
    mdy = dy

    if inp.movement.isDown() and inp.run.isDown() and @canRun()
      mdx *= @runningSpeed
      mdy *= @runningSpeed
      @stammina -= @runningCost
    else
      mdx *= @walkingSpeed
      mdy *= @walkingSpeed
      @rest()

    @x += mdx
    @y += mdy

    @x += @edx
    @y += @edy

    @g.vx = -@x | 0
    @g.vy = -@y | 0

    spr.current = @waiting if not inp.movement.isDown()

    
    
    @pickupCooldown -= 1

    if inp.pickup.isDown() and @pickupCooldown <= 0
      @pickupCooldown = 10
      if @isCarrying == null
        x0 = @x
        y0 = @y
        nearby = @g.getEntities (e) ->
          return false if not e.hasTag "item"
          
          dx = x0 - e.x
          dy = y0 - e.y
          length = dx * dx + dy * dy

          if length < 64 * 64
            true
          else 
            false

        if nearby.length != 0
          nearby[0].pickup @
      else
        if dx == 0 and dy == 0
          @isCarrying.hurl @, 0, 0
        else
          if @hasStammina 20.0 * @isCarrying.weight
            @isCarrying.hurl @, dx * @strength, dy * @strength
          
        

  renderUI : ->
    if not @isDead()
      #render UI
      x = -400
      y = -200

      @g.shapeRender.drawRectFilled x + 6, y + 6, x + 6 + 92 * (@stammina / @maxStammina), y + 18, 1.0, 1.0, 0.0, 1.0
      @g.shapeRender.drawLine x + 4, y + 4, x + 100, y + 4, 0.0, 0.0, 0.0, 1.0
      @g.shapeRender.drawLine x + 4, y + 20, x + 100, y + 20, 0.0, 0.0, 0.0, 1.0
      @g.shapeRender.drawLine x + 4, y + 4, x + 4, y + 20, 0.0, 0.0, 0.0, 1.0
      @g.shapeRender.drawLine x + 100, y + 4, x + 100, y + 20, 0.0, 0.0, 0.0, 1.0

      x = -400
      y = -170

      @g.shapeRender.drawRectFilled x + 6, y + 6, x + 6 + 92 * (@health / @maxHealth), y + 18, 1.0, 0.0, 0.0, 1.0
      @g.shapeRender.drawLine x + 4, y + 4, x + 100, y + 4, 0.0, 0.0, 0.0, 1.0
      @g.shapeRender.drawLine x + 4, y + 20, x + 100, y + 20, 0.0, 0.0, 0.0, 1.0
      @g.shapeRender.drawLine x + 4, y + 4, x + 4, y + 20, 0.0, 0.0, 0.0, 1.0
      @g.shapeRender.drawLine x + 100, y + 4, x + 100, y + 20, 0.0, 0.0, 0.0, 1.0

  die : () ->
    screen = new window.LooseScreen()
    window.makeCurrentScreen screen

  render : () ->
    @transform.setTranslation @x, @y
    if @isCarrying == null
      @g.draw.drawSprite spr.getFrame(), @transform 
    else
      [x, y] = spr.getFrame()
      @g.draw.drawSprite [x, y + 32], @transform 

    @renderUI()    


  slideCollide : (s) ->
    return if @ghost
    @x = s[0]
    @y = s[1]
    
    @g.vx = -@x
    @g.vy = -@y

  collidedWith : (e, s) ->
